package com.yaDuo.ticketUnion.viewmodel

import com.yaDuo.common.base.BaseViewModel
import com.yaDuo.common.request.CategoryDataBean
import com.yaDuo.common.request.MaterialDataBean
import com.yaDuo.common.request.NetRepository
import com.yaDuo.common.util.LogUtil
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

class AppViewModel : BaseViewModel() {

    companion object {
        private const val TAG = "AppViewModel"
    }

    fun init() {}
    private var categoryDataList: List<CategoryDataBean> = mutableListOf()
    private var materialDataList: List<MaterialDataBean> = mutableListOf()

    private val repository by lazy { NetRepository.instance }


    @OptIn(DelicateCoroutinesApi::class)
    fun getCategoryList(): List<CategoryDataBean> {
        GlobalScope.launch(Dispatchers.Main) {
            LogUtil.d(TAG, "getCategoryList begin")
            val response = repository.getCategoryList()
            if (response.data.isNotEmpty()) {
                categoryDataList = response.data
                LogUtil.i(TAG, "getCategoryList = $categoryDataList")
            }
        }
        return categoryDataList
    }

    @OptIn(DelicateCoroutinesApi::class)
    fun getMaterialList(): List<MaterialDataBean> {
        GlobalScope.launch(Dispatchers.Main) {
            LogUtil.d(TAG, "getMaterialList begin")
            val response = repository.getMaterialList()
            if (response.data.isNotEmpty()) {
                materialDataList = response.data
                LogUtil.i(TAG, "getMaterialList =$materialDataList")
            }
        }
        return materialDataList
    }

}